package com.syqc.monitor.ui.alarm

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.PopupWindow
import androidx.core.view.isGone
import com.syqc.comlib.utils.toast
import com.syqc.monitor.R
import com.syqc.monitor.databinding.PopupHandleAlarmBinding

/**
 *@Author Jasper
 *@Time 2021/9/27 17:28
 *@Desc
 */
class AlarmHandlePopup(
    private val isAllAlarms: Boolean,
    private val context: Context,
    private val textBack: () -> Unit,
    private val callback: (String) -> Unit
) : PopupWindow() {
    private val binding = PopupHandleAlarmBinding.inflate(LayoutInflater.from(context))


    init {
        contentView = binding.root
        isClippingEnabled = false
        isFocusable = true //popupwindow设置焦点
        setBackgroundDrawable(ColorDrawable(-0x56000000)) //设置背景
        width = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的宽
        height = LinearLayout.LayoutParams.MATCH_PARENT //设置SelectPicPopupWindow弹出窗体的高
        setListener()
    }

    fun setListener() {
        binding.llCar.isGone = isAllAlarms

        binding.tvPositive.setOnClickListener {
            dismiss()
        }
        binding.tvNegative.setOnClickListener {
            val text = binding.editHandle.text.toString()
            if (text.isNullOrEmpty()) {
                R.string.hint_toast_handle.toast(context)
                return@setOnClickListener
            }
            callback(binding.editHandle.text.toString())
        }
        binding.ivHandleText.setOnClickListener {
            textBack()
        }
    }

    fun showAtLocation(parent: View?) {
        showAtLocation(parent, Gravity.NO_GRAVITY, 0, 0)
    }

    fun setData(title: String) {
        binding.tvHandleTitle.text = title
        binding.editHandle.setText("")
    }

    fun setData(title: String, plate: String) {
        binding.tvPlate.text = plate
        binding.tvHandleTitle.text = title
        binding.editHandle.setText("")
    }

    fun setHandText(text: String) {
        binding.editHandle.setText(text)
        binding.editHandle.setSelection(binding.editHandle.text.length)
    }

}